package org.com.yz.dubbo.web.ctrl;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringEscapeUtils;
import org.com.yz.common.annotion.Parameter;
import org.com.yz.common.constant.PayConstant;
import org.com.yz.common.util.ClassRefUtil;
import org.com.yz.common.util.MyLog;
import org.com.yz.common.util.ToolUtil;
import org.com.yz.dal.dao.model.Request007;
import org.com.yz.dal.dao.model.WithDraw;
import org.com.yz.dubbo.web.service.RpcCommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 提现接口控制类
 *
 * @author
 * @date
 */
@RestController
public class WithDrawMchAccntController {

    private final MyLog _log = MyLog.getLog(WithDrawMchAccntController.class);
    @Autowired
    private RpcCommonService rpcCommonService;


    /**
     * 提现
     *
     * @param data
     * @param sign
     * @return
     */
    @PostMapping(value = "/api/withDrawMchAccnt.htm")
    @Parameter
    public String withDrawMchAccnt(String data, String sign) {
        String result = StringEscapeUtils.unescapeJava(data);
        Request007 rq007 = JSONObject.parseObject(result, Request007.class);
        Request007 res = new Request007();

        try {
            //执行提现逻辑
            WithDraw withDraw = null;
            //订单号已存在
            withDraw = rpcCommonService.withDrawMchAccntService.checkWithDraw(rq007.getBizContent().getOrderNo(), rq007.getBizContent().getMchAccntNo(), rq007.getMchNo());
            if (ToolUtil.isNotEmpty(withDraw)) {
                res.setBizContent(withDraw);
                return ClassRefUtil.createRetData(rq007, res, PayConstant.RETURN_DEPOSIT_ORDER_NO_EXIST, PayConstant.RETURN_DEPOSIT_ORDER_NO_EXISTS);//订单号重复
            } else {
                withDraw = rpcCommonService.withDrawMchAccntService.withDraw(data);
                res.setBizContent(withDraw);
                if (withDraw != null && withDraw.getStatus().equals("2")) {//提现失败
                    return ClassRefUtil.createRetData(rq007, res, PayConstant.RETURN_DEPOSIT_WITHDRAW_FAIL, withDraw.getErrmsg());
                }
                return ClassRefUtil.createRetData(rq007, res, PayConstant.RETURN_DEPOSIT_SUCESS, PayConstant.RETURN_ALIPAY_VALUE_SUCCESS);
            }
        } catch (Exception e) {
            _log.error(e, ToolUtil.getExceptionMsg(e));
            System.out.println(ToolUtil.getExceptionMsg(e));
            if(ToolUtil.getExceptionMsg(e).contains(PayConstant.RETURN_DEPOSIT_TIMEOUT_ERRORS)){
                return ClassRefUtil.createRetData(rq007, res, PayConstant.RETURN_DEPOSIT_TIMEOUT_ERROR, PayConstant.RETURN_DEPOSIT_TIMEOUT_ERRORS);
            }
            return ClassRefUtil.createRetData(rq007, res, PayConstant.RETURN_DEPOSIT_CODE_ERROR, PayConstant.RETURN_DEPOSIT_CODES_ERROR);
        } finally {

        }
    }




    /**
     * 事务包裹网络请求测试
     * @param data
     * @param sign
     * @return
     */
    @PostMapping(value = "/api/httptest.htm")
    public String transactionalTest(String data, String sign) {
        try{
            rpcCommonService.withDrawMchAccntService.testTransactional();
        }catch (Exception e){
            _log.error(e, ToolUtil.getExceptionMsg(e));
        }
        return "success";
    }


}
